package com.hqq.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;


/**
 * 对象类型消息的队列、交换机、路由、绑定等对象配置类
 * @author huangqiqin
 * @date 2021/04/16 22:00
 **/
@Configuration
public class ObjectMessageConfig {

    /**
     * 注入配置文件的环境变量实例
     */
    @Autowired
    private Environment env;

    /**
     * 创建队列：可持久化的队列
     * @return
     */
    @Bean(name = "objectQueue")
    public Queue objectQueue(){
        return new Queue(env.getProperty("mq.object.queue.name"), true);
    }

    /**
     * 创建交换机：可持久化数据的交换机
     * @return
     */
    @Bean(name = "objectExchange")
    public DirectExchange objectExchange(){
        return new DirectExchange(env.getProperty("mq.object.exchange.name"), true, false);
    }

    /**
     * 创建绑定：将队列绑定到交换机上，并指定路由key
     * @param objectQueue       注入上面创建的队列
     * @param objectExchange    注入上面创建的交换机
     * @return
     */
    @Autowired
    @Bean(name = "objectBinding")
    public Binding objectBinding(Queue objectQueue, DirectExchange objectExchange){
        return BindingBuilder
                .bind(objectQueue)    // 绑定objectQueue队列
                .to(objectExchange)   // 到objectExchange交换机上
                .with(env.getProperty("mq.object.routing.key.name"));   //指定路由key
    }
}
